﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace UFSSWebApp
{
    public partial class popsubs : System.Web.UI.Page
    {

        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            try
            {
                refresh();
                lblError.Visible = false;
            }
            catch
            {
                lblError.Visible = true;
                lblError.Text = "There was a problem completing your request... please try again later";
            }
            refresh();
        }
        private void refresh()
        {

            UFSSservicereference.UFSSSubscription[] subs = ussc.getTopSubs(uc, 10);

            //create dataset
            DataSet ds = new DataSet();
            DataTable dt = new DataTable("subs");
            DataRow dr;

            //create columns
            DataColumn namecolumn, filescolumn, datecolumn, ownercolumn, memberscolumn;
            datecolumn = new DataColumn("Last update");
            namecolumn = new DataColumn("Subscription name");
            filescolumn = new DataColumn("Files");
            ownercolumn = new DataColumn("Owner");
            memberscolumn = new DataColumn("Members");




            //insert columns
            dt.Columns.Add(datecolumn);
            dt.Columns.Add(namecolumn);
            dt.Columns.Add(filescolumn);
            dt.Columns.Add(ownercolumn);
            dt.Columns.Add(memberscolumn);



            //create headers

            //fill table
            foreach (UFSSservicereference.UFSSSubscription sub in subs)
            {
                dr = dt.NewRow();
                dr["Subscription name"] = sub.subName;
                dr["Files"] = sub.files;
                dr["Owner"] = sub.ownername;
                dr["Last update"] = sub.lastup.ToString();
                if (sub.lastup.ToString().Equals("1/1/0001 12:00:00 AM"))
                    dr["Last update"] = "never";
                dr["Members"] = sub.members;


                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);

            //populate gridview
            DataView dv = new DataView(ds.Tables["subs"]);
            dv = ds.Tables[0].DefaultView;


            gvPopSubs.DataSource = ds.Tables["subs"];
            gvPopSubs.AutoGenerateColumns = true;
            gvPopSubs.DataBind();
            for (int i = 0; i < subs.Length; i++)
            {
                gvPopSubs.Rows[i].Cells[1].Text = "<a href=\"usersubs.aspx?id=" + subs[i].subid + "\">" + subs[i].subName + "</a>";
                gvPopSubs.Rows[i].Cells[3].Text = "<a href=\"userpage.aspx?id=" + gvPopSubs.Rows[i].Cells[3].Text + "\">" + gvPopSubs.Rows[i].Cells[3].Text + "</a>";
            }
        }
    }
}